<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />



<?php


function printRowUser($row,$delete=false)
{
	$time1 = $row[1];	
	$datatimeArray= explode(" ",$time1);
	$thetimepart = $datatimeArray[1];
	
	$content = $row[2];
	$id = $row[0];
	if($delete==true){
		printf("<tr><td><form  action='updatetodoitem.php?itemid=%d' method='post' autocomplete='off' >
					<input name='todotime' class='todotime' type='text' value='%s' />
					<input name='content' class='inputtext' type='text' id='content' value='%s' size='100' />
					<input type='submit' name='button' id='button' value='更新' />
					</form><td><a href='deletetodoitem.php?itemid=%d'><img src='../delete.png' /></a></td></td></tr>", $id,$thetimepart,$content,$id);		
	}
	else
	{
		printf("<tr><td><form  action='updatetodoitem.php?itemid=%d' method='post' autocomplete='off' >
					<input name='todotime' type='text' value='%s' />
					<input name='content' class='inputtext' type='text' id='content' value='%s' size='100' />	
					</form></td></tr>", $id,$thetimepart,$content);	
	}
}

function printRowAuto($row)
{
	$clientid = $row[2];
	$name = $row[1];
	
	$clientstr = sprintf("拜访客户<a href ='clientinfo.php?clientid=%d' rel='facebox'>$name</a>",$clientid,$name);	
	
	
	$clienttime = $row[0];
	$clientdatatime =explode(" ",$clienttime);
	$clienttimepart = $clientdatatime[1];
	printf("<tr><td><input type='text' class='inputtext' value='%s' readonly='readonly' /> ",$clienttimepart); 
	echo " ";
	echo $clientstr;
	echo "</td></tr>";
}
function ListTodoList($salerid, $DB, $offsetday)
{
	$query = "SELECT id,timetodo,content FROM todolist WHERE date(timetodo)=date(now())+$offsetday AND salerid =$salerid   ORDER BY timetodo ASC ";
	
	$query2 = "SELECT next_devtime,name, id,type  FROM client WHERE  date(next_devtime)=date(now())+$offsetday AND id IN (SELECT clientid FROM 		client_saler_rel WHERE salerid=$salerid AND state ='active' ) AND type<>3 ORDER BY next_devtime ASC";
	
	$result = mysql_query($query,$DB);
	$result2 = mysql_query($query2,$DB);
	echo "<table cellspacing='1' cellpadding='1' >";
	$row1 = mysql_fetch_row($result); $row2 = mysql_fetch_row($result2);
	
	while(true)
	{
		if(!$row2) //print all result1;
		{
			while($row1)
			{
				printRowUser($row1,$offsetday==0);
				$row1 = mysql_fetch_row($result);
			};
			break;
		}
		
		if(!$row1)
		{
			while($row2)
			{
				printRowAuto($row2);
				$row2 = mysql_fetch_row($result2);
			};
			break;
		}
		
		$time1 = $row1[1];
		$time2 = $row2[0];
		
		if($time1 < $time2) //print result1
		{
			printRowUser($row1);
			$row1 = mysql_fetch_row($result);
			continue;
		}
		else //print result2
		{
			printRowAuto($row2);
			$row2 = mysql_fetch_row($result2);
			continue;
		}	
	}
	
	if($offsetday==0)
	{
		echo "<tr><td><form action='updatetodoitem.php?itemid=-1' method='post' autocomplete='off'>
				<input name='todotime' class='todotime' type='text' />
				<input name='content'  class='inputtext' type='text' id='content' size='100' />
				<input type='submit' name='button' id='button' value='添加' />
				</form></td></tr>";	
	}
	echo "</table>";	
}


function listClientToCall($salerid, $DB, $offsetday)
{
	$query = "SELECT id,timetodo,content FROM todolist WHERE date(timetodo)=date(now())+$offsetday AND salerid =$salerid   ORDER BY timetodo ASC ";
	
	$query2 = "SELECT next_devtime,name, id,type  FROM client WHERE  date(next_devtime)=date(now())+$offsetday AND id IN (SELECT clientid FROM 		client_saler_rel WHERE salerid=$salerid AND state ='active' ) AND type<>3 ORDER BY next_devtime ASC";
	
	$result = mysql_query($query,$DB);
	$result2 = mysql_query($query2,$DB);
	$row1 = mysql_fetch_row($result); $row2 = mysql_fetch_row($result2);
	
	
	echo "<table cellspacing='1' cellpadding='1' >";
	while(true)
	{
		if(!$row2) //print all result1;
		{
			while($row1)
			{
				printRowUser($row1,$offsetday==0);
				$row1 = mysql_fetch_row($result);
			};
			break;
		}
		
		if(!$row1)
		{
			while($row2)
			{
				printRowAuto($row2);
				$row2 = mysql_fetch_row($result2);
			};
			break;
		}
		
		$time1 = $row1[1];
		$time2 = $row2[0];
		
		if($time1 < $time2) //print result1
		{
			printRowUser($row1);
			$row1 = mysql_fetch_row($result);
			continue;
		}
		else //print result2
		{
			printRowAuto($row2);
			$row2 = mysql_fetch_row($result2);
			continue;
		}	
	};	
	
	echo "</table>";
}
?>

</head>